package tictactoe.clientprocessors;

import tictactoe.Client;
import tictactoe.protocol.Protocol;

import java.io.IOException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 15.01.12
 * Time: 21:01
 * To change this template use File | Settings | File Templates.
 */
public class SingleClientProcessor implements Runnable,ClientProcessor
{
	ArrayList<Client> clients = new ArrayList<Client>();
	HashMap<Runnable, Thread> clientThreads = new HashMap<Runnable, Thread>();
	private ArrayList<Client> preAuthClient = new ArrayList<Client>();
	private boolean flag=true;

	public SingleClientProcessor()
	{
		//new Thread(this).start();
	}

	private void addPreAuthClient(Client client)
	{
		preAuthClient.add(client);
		System.out.println("preAuth client added");
	}
	public ArrayList<Client> getPreAuthClient()
	{
		return preAuthClient;
	}
	@Override
	public void addClient(Client c)
	{
		//addPreAuthClient(c);
		clients.add(c);
		Logger.getLogger("TicTacToeServer").log(Level.SEVERE,"Client added to SingleClientProcessor list: "+c.getName());
		//clientThreads.put(c,t);
	}
	private void checkValid(Client c)
	{
		if(isValid(c))
		{
			clients.add(c);
			Thread t = new Thread(c);
			t.start();

				c.sendMsg(Protocol.encode(Protocol.status, Protocol.authPassed));
		}else
		{
			try
			{
				c.sendMsg(Protocol.encode(Protocol.status, Protocol.authFail));
				c.disconnect();
			} catch (IOException e)
			{
				e.printStackTrace();
				Logger.getLogger("TicTacToeServer").log(Level.SEVERE,e.getMessage());
			}
		}
	}

	@Override
	public void removeClient(Client c)
	{
		clients.remove(c);
		/*try
		{
			clientThreads.get(c.getName()).wait();

		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}*/

	}

	public boolean isValid(Client client)
	{
		boolean ret = true;
		for(Client c:clients)
		{
			if(client.getName().equalsIgnoreCase(c.getName()))
			{
				ret = false;
			}
		}
		return ret;

	}

	@Override
	public ArrayList<Client> getClients()
	{
		return clients;
	}

	@Override
	public void run()
	{
		while(flag)
		{


			/*for(int i=0;i<preAuthClient.size();i++)
			{

					System.out.println("Lol");
					if(preAuthClient.get(i).hasMessage())
					{
						System.out.println("PreAuth client "+preAuthClient.get(i)+" has message");
						Protocol.decodeSystemPacket(preAuthClient.get(i).getMessage(),preAuthClient.get(i),preAuthClient);
					}

				if(preAuthClient.get(i).getName()!=null)
				{
					checkValid(preAuthClient.get(i));
					preAuthClient.remove(preAuthClient.get(i));
				}
			} */
			/*try{
			for(Client c:preAuthClient)
			{
				if(c.hasMessage())
				{
					System.out.println("PreAuth client "+c+" has message");
					Protocol.decodeSystemPacket(c.getMessage(),c,preAuthClient);
				}
				if(c.getName()!=null)
				{
					checkValid(c);
					preAuthClient.remove(c);
				}

			}
			}catch(ConcurrentModificationException e)
			{
				Logger.getLogger("TicTacToeServer").log(Level.SEVERE,e.getMessage());
			} */


		}
	}

	@Override
	public synchronized void getClientForGame(Client c)
	{
		/*try
		{
			clientThreads.get(c).wait();
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}  */
		c.setFlag(false);
		Logger.getLogger("TicTacToeServer").log(Level.SEVERE,"Client is taken from SingleClientProcessor list te new game: "+c.getName());
		
	}

	@Override
	public synchronized void returnClient(Client c)
	{
		/* clientThreads.get(c).notify();*/
		c.setFlag(true);
		new Thread(c).start();
		Logger.getLogger("TicTacToeServer").log(Level.SEVERE,"Client returned to SingleClientProcessor list: "+c.getName());
	}
}
